package de.yellowshoes.tedx.net;

import java.util.List;

import android.util.Log;
import de.yellowshoes.tedx.TEDxConstants;
import de.yellowshoes.tedx.net.exception.CsvReaderException;
import de.yellowshoes.tedx.persistence.entity.Eventdetail;

/**
 * Reads speaker infos from a csv file.
 * 
 * @author calibanatspace
 * 
 */
public class EventdetailReader
    {
    private String url = null;

    public EventdetailReader( String url )
        {
        this.url = url;
        }

    public Eventdetail read()
        {
        CsvRemoteReader reader = new CsvRemoteReader( url, ";", 6 );
        List< Object[] > objectList = null;

        try
            {
            objectList = reader.read();
            }
        catch( CsvReaderException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Error while reading eventdetail.csv via url " + url, e );
            return null;
            }

        if( objectList.size() > 0 )
            {
            objectList.remove( 0 );
            }

        Eventdetail eventdetail = null;

        if( objectList.size() > 0 )
            {
            Object[] objects = objectList.get( 0 );
            eventdetail = new Eventdetail();
            eventdetail.setPlaceName( ( String )objects[ 0 ] );
            eventdetail.setAdress( ( String )objects[ 1 ] );
            eventdetail.setDescription( ( String )objects[ 2 ] );
            eventdetail.setWebsideUrl( ( String )objects[ 3 ] );
            eventdetail.setLatitude( objects[ 4 ] != null ? Double.parseDouble( ( String )objects[ 4 ] ) : null );
            eventdetail.setLongitude( objects[ 5 ] != null ? Double.parseDouble( ( String )objects[ 5 ] ) : null );
            }

        return eventdetail;
        }
    }
